﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI.WebControls;
using btnet;
using System.Data;

/// <summary>
/// Summary description for delete_comment
/// </summary>
public partial class delete_comment : System.Web.UI.Page
{
    protected String sql;

    protected Security security;

    protected void Page_Init(object sender, EventArgs e)
    {
        ViewStateUserKey = Session.SessionID;
    }

    ///////////////////////////////////////////////////////////////////////
    void Page_Load(Object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            String footer_label = null;
            if (Application["custom_footer"] != null)
            {
                footer_label = (String)Application["custom_footer"];
            }
            custom_footer_label.Text = footer_label;

            Label bug = this.FindControl("singular_bug_label") as Label;
            bug.Text = (Util.get_setting("SingularBugLabel", "bug"));
        }

        Util.do_not_cache(Response);

        security = new Security();

        security.check_security(HttpContext.Current, Security.ANY_USER_OK_EXCEPT_GUEST);

        if (security.user.is_admin || security.user.can_edit_and_delete_posts)
        {
            //
        }
        else
        {
            Response.Write("You are not allowed to use this page.");
            Response.End();
        }

        if (IsPostBack)
        {
            // do delete here

            sql = @"delete bug_posts where bp_id = $1";
            sql = sql.Replace("$1", row_id.Value);
            btnet.DbUtil.execute_nonquery(sql);
            Response.Redirect("edit_bug.aspx?id=" + redirect_bugid.Value);
        }
        else
        {

            string bug_id = Util.sanitize_integer(Request["bug_id"]);
            redirect_bugid.Value = bug_id;

            int permission_level = btnet.Bug.get_bug_permission_level(Convert.ToInt32(bug_id), security);
            if (permission_level != Security.PERMISSION_ALL)
            {
                Response.Write("You are not allowed to edit this item");
                Response.End();
            }

            titl.InnerText = Util.get_setting("AppTitle", "BugTracker.NET") + " - "
                + "delete comment";

            string id = Util.sanitize_integer(Request["id"]);

            back_href.HRef = "edit_bug.aspx?id=" + bug_id;

            sql = @"select bp_comment from bug_posts where bp_id = $1";
            sql = sql.Replace("$1", id);

            DataRow dr = btnet.DbUtil.get_datarow(sql);

            // show the first few chars of the comment
            string s = Convert.ToString(dr["bp_comment"]);
            int len = 20;
            if (s.Length < len) { len = s.Length; }

            confirm_href.InnerText = "confirm delete of comment: "
                    + s.Substring(0, len)
                    + "...";

            row_id.Value = id;
        }


    }
}